package com.bolt.auth.admin.api.sys.dto.converter;


import com.bolt.auth.admin.api.sys.dto.UserDTO;
import com.bolt.auth.admin.api.sys.entity.RoleEntity;
import com.bolt.auth.admin.api.sys.entity.UserEntity;
import com.bolt.common.reflect.ObjectUtil;
import com.bolt.common.utils.StrUtil;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/27.
 */
public class UserConverter {
    private UserConverter() {
// do nothing
    }

    /**
     * DO对象转换为DTO对象
     *
     * @param user DO对象
     * @return BO对象
     */
    public static UserDTO toDTO(UserEntity user) {
        if (ObjectUtil.isNull(user)) {
            return null;
        }
        UserDTO result = new UserDTO();
        result.setId(user.getId());
        result.setUserName(user.getUserName());
        result.setRealName(user.getRealName());
        result.setNickName(user.getNickName());
        result.setPassword(user.getPassword());
        result.setEmail(user.getEmail());
        result.setMobile(user.getMobile());
        result.setPwdResetTime(user.getPwdResetTime());
        result.setEnabled(user.getEnabled());
        result.setAdmin(user.getEnabled());
        result.setAuthSalt(user.getAuthSalt());
        result.setOrgId(user.getOrgId());
        result.setAvatarImg(user.getAvatarPath());
        result.setVersionNO(user.getVersionNO());
        if (ObjectUtil.isNotNull(user.getOrgInfo())) {
            result.setOrgName(user.getOrgInfo().getName());
        }
        List<RoleEntity> roles = user.getRoles();
        result.setRoleIds( StrUtil.join(
                roles.stream().map(k->k.getId()).collect(Collectors.toList()),",")
        );
        return result;
    }

    /**
     * DTO对象转换为DO对象
     *
     * @param user DO对象
     * @return VO对象
     */
    public static UserEntity toEntity(UserDTO user) {
        if (ObjectUtil.isNull(user)) {
            return null;
        }
        UserEntity result = new UserEntity();
        result.setId(user.getId());
        result.setUserName(user.getUserName());
        result.setRealName(user.getRealName());
        result.setNickName(user.getNickName());
        result.setPassword(user.getPassword());
        result.setEmail(user.getEmail());
        result.setMobile(user.getMobile());
        result.setPwdResetTime(user.getPwdResetTime());
        result.setEnabled(user.getEnabled());
        result.setAdmin(user.getAdmin());
        result.setAuthSalt(user.getAuthSalt());
        result.setOrgId(user.getOrgId());
        result.setAvatarPath(user.getAvatarImg());

        result.setVersionNO(user.getVersionNO());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为DO对象
     *
     * @param user DO对象
     * @return VO对象
     */
    public static UserEntity toEntity(UserEntity result, UserDTO user) {
        if (Objects.isNull(user)) {
            return null;
        }
        result.setUserName(user.getUserName());
        result.setRealName(user.getRealName());
        result.setNickName(user.getNickName());
        result.setEmail(user.getEmail());
        result.setMobile(user.getMobile());
        result.setEnabled(user.getEnabled());
        result.setOrgId(user.getOrgId());
        return result;
    }

    /**
     * 批量 DO对象转换为DTO对象
     *
     * @param userList DO集合对象
     * @return DTO对象
     */
    public static List <UserDTO> toDTO(List <UserEntity> userList) {
        if (Objects.isNull(userList)) {
            return null;
        }
        return userList.stream().map(UserConverter::toDTO).collect(Collectors.toList());
    }

}
